package com.atguigu.gmallrealtime.util;

import com.atguigu.gmallrealtime.common.Constant;
import com.google.common.base.CaseFormat;
import jdk.nashorn.internal.ir.CaseNode;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yhm
 * @create 2023-09-23 10:16
 */
public class JDBCUtil {
    public static Connection getJdbcConnection(String driver,
                                               String url,
                                               String user,
                                               String pw) {
        // 1. 加载驱动
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("驱动没有找到: " + driver);
        }

        // 2. 通过驱动管理器获取链接对象
        try {
            return DriverManager.getConnection(url, user, pw);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("获取jdbc 连接异常: " + e.getMessage());
        }

    }

    public static void closeConnection(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static Connection getMysqlConnection() {
        String driver = Constant.MYSQL_DRIVER;
        String url = Constant.MYSQL_URL;
        return getJdbcConnection(driver, url, "root", "000000");
    }

    /**
     * 使用传入的jdbc连接 执行对应的sql 然后将结果转换为对应的泛型
     *
     * @param connection jdbc连接
     * @param sql        查询数据的sql
     * @param clazz      泛型的末班
     * @param <T>        传入的泛型
     * @return 泛型对象的集合
     */
    public static <T> List<T> queryList(Connection connection, String sql, Class<T> clazz, boolean... isUnderLineToCamel) throws SQLException, IllegalAccessException, InstantiationException, InvocationTargetException {
        // 默认不需要转换下划线到小驼峰
        boolean flag = false;
        if (isUnderLineToCamel != null && isUnderLineToCamel.length > 0){
            flag = isUnderLineToCamel[0];
        }
        // 1. 编译sql
        PreparedStatement ps = connection.prepareStatement(sql);
        ArrayList<T> results = new ArrayList<>();
        // 2. 查询数据
        ResultSet resultSet = ps.executeQuery();

        // 3. 遍历结果
        while (resultSet.next()) {
            // 当前为一行数据  对应一个T的对象
            T t = clazz.newInstance();
            ResultSetMetaData metaData = resultSet.getMetaData();

            for (int i = 1; i <= metaData.getColumnCount(); i++) {

                String columnName = metaData.getColumnName(i);
                if (flag){
                    // 如果传入的参数需要转换从下划线到小驼峰
                    columnName = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL,columnName);
                }
                BeanUtils.setProperty(t, columnName, resultSet.getString(i ));
            }
            results.add(t);
        }
        return results;
    }
}
